package b4.widget.tab
{
	import b4.widget.basic.BasicWidget;
	
	import flash.display.Sprite;

	public class Tab extends BasicWidget
	{
		public var tabPages:Array;
		public var pageSelector:TabPageSelector;
		
		public var tabName:String;
		public var items:Array;
		private var wNum:int;
		private var hNum:int; 
		private var gap:int;
		private var pagePosX:Number;
		private var pagePosY:Number;
		private var _currentPageIndex:int;
		
		public function Tab(name:String, wNum:int = 5, hNum:int = 3, gap:int = 5)
		{
			super();
			this.tabName = name;
			this.wNum = wNum;
			this.hNum = hNum;
			this.gap = gap;
			pagePosX = 0;
			pagePosY = 0;
			this.tabPages = new Array();
			this.items = new Array();
			_currentPageIndex = 0;
		}
		
		public function addPageSelector(TabPageFirstPage:Class, TabPagePageUp:Class, TabPagePageDown:Class, TabPageLastPage:Class):void
		{
			pageSelector = new TabPageSelector(this, TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage);
			addChild(pageSelector);
		}
		
		public function setPageSelectorPos(posX:Number, posY:Number):void
		{
			pageSelector.x = posX;
			pageSelector.y = posY;
		}
		
		public function setPageSelectorStyle(TabPageFirstPage:Class, TabPagePageUp:Class, TabPagePageDown:Class, TabPageLastPage:Class, 
			pageNumColor:uint, currentPageNumColor:uint):void
		{
			removeChild(pageSelector);
			pageSelector = new TabPageSelector(this, TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, pageNumColor, currentPageNumColor);
			addChild(pageSelector);
		}
		
		public function setPagePos(posX:Number, posY:Number):void
		{
			this.pagePosX = posX;
			this.pagePosY = posY;
			for (var i:int = 0; i < tabPages.length; i++)
			{
				var tabPage:TabPage = tabPages[i];
				tabPage.x = pagePosX;
				tabPage.y = pagePosY;
			}
		}
		
		public function addItem(item:Sprite, width:Number, height:Number):void
		{
			items.push(item);
			if (items.length > wNum * hNum * this.tabPages.length)
			{
				var tabPage:TabPage = new TabPage(wNum, hNum, gap)
				if (currentPageIndex != this.tabPages.length)
				{
					tabPage.visible = false;
				}					
				tabPage.x = pagePosX;	
				tabPage.y = pagePosY;
				tabPages.push(tabPage);
				addChild(tabPage);
				pageSelector.refreshTotalPageNum();
			}
			tabPages[tabPages.length - 1].addItem(item, width, height);
		}

		public function removeAll():void
		{
			while (tabPages.length > 0)
			{
				var tabPage:TabPage = tabPages.pop();
				removeChild(tabPage);
			}
			_currentPageIndex = 0;
			items = new Array();
		}
		
		public function get totalPageNum():int
		{
			return tabPages.length;
		}
		
		public function set currentPageIndex(pageIndex:int):void
		{
			pageSelector.pageNumArea.refreshCurrentPageIndex(pageIndex);
			if (pageIndex != _currentPageIndex)
			{
				tabPages[_currentPageIndex].visible = false;
				tabPages[pageIndex].visible = true;
				_currentPageIndex = pageIndex;
			} 
		}
		
		public function get currentPageIndex():int
		{
			return _currentPageIndex;
		}
		
	}
}